package com.cn.onetrip.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;

public class ScenicGuiderActivity extends Activity {

	public static final String TAG = "test";
	public ViewPager viewpager = null;
	public List<View> list = null;
	public ImageView[] img = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (SysApplication.visitsListObj.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		SharedPreferences remindShared = getSharedPreferences("GuiderActivity",
				0);
		boolean isFirstGuider = remindShared.getBoolean("isFirstEnter", false);
		if (isFirstGuider) {
			Intent intent = new Intent(getApplicationContext(),
					TripActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			return;
		}

		setContentView(R.layout.activity_guider);
		remindShared = getSharedPreferences("GuiderActivity", 0);
		remindShared.edit().putBoolean("isFirstEnter", true).commit();
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		list = new ArrayList<View>();
		list.add(getLayoutInflater().inflate(R.layout.guider_tab1, null));
		list.add(getLayoutInflater().inflate(R.layout.guider_tab2, null));
		list.add(getLayoutInflater().inflate(R.layout.guider_tab3, null));
		View view = getLayoutInflater().inflate(R.layout.guider_tab4, null);
		list.add(view);

		Button enter = (Button) view.findViewById(R.id.enter_content);
		enter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						TripActivity.class);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});

		img = new ImageView[list.size()];
		LinearLayout layout = (LinearLayout) findViewById(R.id.viewGroup);
		LinearLayout.LayoutParams searchParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		searchParams.leftMargin = 10;
		for (int i = 0; i < list.size(); i++) {
			img[i] = new ImageView(ScenicGuiderActivity.this);
			if (0 == i) {
				img[i].setBackgroundResource(R.drawable.guider_dian_up);
			} else {
				img[i].setBackgroundResource(R.drawable.guider_dian_down);
			}
			layout.addView(img[i], searchParams);
		}
		viewpager.setAdapter(new ViewPagerAdapter(list));
		viewpager.setOnPageChangeListener(new ViewPagerPageChangeListener());
	}

	class ViewPagerAdapter extends PagerAdapter {

		private List<View> list = null;

		public ViewPagerAdapter(List<View> list) {
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(list.get(position));
			return list.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(list.get(position));
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}

	class ViewPagerPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int state) {
		}

		@Override
		public void onPageScrolled(int page, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int page) {
			for (int i = 0; i < list.size(); i++) {
				if (page == i) {
					img[i].setBackgroundResource(R.drawable.guider_dian_up);
				} else {
					img[i].setBackgroundResource(R.drawable.guider_dian_down);
				}
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(getApplicationContext(),
					TripActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

}
